package org.thss.petri.gef.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.thss.petri.model.Node;
import org.thss.petri.model.NodeConstraint;
import org.thss.petri.model.PetriFactory;

public class MoveNodeCommand extends Command {
	private Node node;
	private NodeConstraint oldConstraint;
	private NodeConstraint newConstraint;
	
	public Node getNode() {
		return node;
	}
	public void setNode(Node node) {
		this.node = node;
	}

	public void setConstraint(Rectangle r) {
		this.newConstraint = createNodeConstraint(r);
	}

	@Override
	public void execute() {
		oldConstraint = node.getConstraint();
		node.setConstraint(newConstraint);
	}
	
	@Override
	public void redo() {
		node.setConstraint(newConstraint);
	}
	@Override
	public void undo() {
		node.setConstraint(oldConstraint);
	}
	
	private NodeConstraint createNodeConstraint(Rectangle r) {
		NodeConstraint c = PetriFactory.eINSTANCE.createNodeConstraint();
		c.setX(r.x);
		c.setY(r.y);
		c.setWidth(r.width);
		c.setHeight(r.height);
		return c;
	}
	
}
